package cn.myframe.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Scanner;

import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class ShellUtil {
	
	private static final String USER="root";
    private static final String PASSWORD="123456";
    private static final String HOST="192.168.32.128";
    private static final int DEFAULT_SSH_PORT=22;
 
    private static  boolean b = true;
    public static void main(String[] arg){
    	
    	int time =20;
        try{
        	  Scanner scan = new Scanner(System.in);
        	  String read = scan.nextLine();
        	  System.out.println("输入数据："+read); 
           
        	  JSch jsch=new JSch();

            
            Session session = jsch.getSession(USER,HOST,DEFAULT_SSH_PORT);
            session.setPassword(PASSWORD);

            // username and password will be given via UserInfo interface.
          //  session.setUserInfo(new MyUserInfo());
            session.connect();
            ChannelShell channel = (ChannelShell) session.openChannel("shell");
            channel.connect();
            InputStream inputStream = channel.getInputStream();
            OutputStream outputStream = channel.getOutputStream();
            new Thread(){
            	public void run(){
            		
            		 try {
            			 Thread.sleep(10*1000);
						outputStream.write("ls \n\r".getBytes());
						 outputStream.flush();
						outputStream.write("ls \n\r".getBytes());
						 outputStream.flush();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
            	}
            }.start();
            
            String cmd = "ls \n\r";
            outputStream.write(cmd.getBytes());
            String cmd2 = "cd /home/jenkins/workspace/ggservice \n\r";
            outputStream.write(cmd2.getBytes());
            String cmd3 = "pwd \n\r";
            outputStream.write(cmd3.getBytes());
            outputStream.flush();
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            new Thread(){
            	public void run(){
            		
            		 try {
            			 int t = 0;
						 while(time>t){
							 t++;
							 Thread.sleep(1*1000);
						 }
						 in.close();
				         session.disconnect();
						 b = false;
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
            	}
            }.start();
            String msg = null;
            while(b&&(msg = in.readLine())!=null){
                System.out.println(msg);
            }
            in.close();
            session.disconnect();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
    
    public static ChannelShell getShellChannel(String user, String host, int port ,String password){
    	 ChannelShell channel = null;
		try {
			JSch jsch=new JSch();
	        Session session = jsch.getSession(user,host,port);
	        session.setPassword(password);
	     //   session.setUserInfo(new MyUserInfo());
	        session.connect();
	        channel = (ChannelShell) session.openChannel("shell");
	        channel.connect();
		} catch (JSchException e) {
			e.printStackTrace();
		}
        return channel;
    }
    
    public static String sendCmd(ChannelShell channel){
    	try {
			InputStream in = channel.getInputStream();
	        OutputStream outputStream = channel.getOutputStream();
	        String cmd = "ls \n\r";
	        outputStream.write(cmd.getBytes());
	        String cmd2 = "cd /home/jenkins/workspace/ggservice \n\r";
	        outputStream.write(cmd2.getBytes());
	        String cmd3 = "pwd \n\r";
	        outputStream.write(cmd3.getBytes());
	        outputStream.flush();
	     //   BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

	      /*  String msg = null;
	        int i = 0;
	        while((msg = in.readLine())!=null){
	            System.out.println(msg);
	            if(i>5){
	            	 break;
	            }
	            i++;
	        }*/
	        byte[] tmp=new byte[1024];
            while(true){
                while(in.available()>0){
                    int i=in.read(tmp, 0, 1024);
                    if(i<0)break;
                    System.out.print(new String(tmp, 0, i));
                }
                if(channel.isClosed()){
                    if(in.available()>0) continue;
                    System.out.println("exit-status: "+channel.getExitStatus());
                    break;
                }
                try{Thread.sleep(1000);}catch(Exception ee){}
            }
	        in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
        
    //    session.disconnect();
    	return null;
    }


    
	
}

